package com.cljz.makepassenger.present.activity;

import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.base.Constant;
import com.cljz.makepassenger.model.net.api.Api;
import com.cljz.makepassenger.model.net.bean.ResponseBean;
import com.cljz.makepassenger.model.net.bean.ThirdLoginBindData;
import com.cljz.makepassenger.ui.activity.BindPhoneActivity;
import com.cljz.makepassenger.utils.L;
import com.clzj.makepassenger.bean.UserInfo;
import com.google.gson.Gson;

import java.util.HashMap;

import cn.droidlover.xdroidmvp.cache.SharedPref;
import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.net.diy.Base64Utils;
import okhttp3.RequestBody;

/**
 * Created by apple on 2017/4/5.
 */

public class BindPhoneP extends BaseP<BindPhoneActivity> {
    public void getCode(String phone) {
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("send_type", Constant.SmsType.SMS_BINDING);
        paramsMap.put("phone", phone);
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getSmsSendData(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())         //异常处理
                .compose(XApi.<ResponseBean>getScheduler())              //线程切换
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(ResponseBean smsSendBean) {
                        if ("0".equals(smsSendBean.getError_code())) {
                            getV().getSmsSucceed();
                        } else if ("1000".equals(smsSendBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getSmsFail(smsSendBean.getMsg());
                        }

                    }
                });

    }

    public void loginBind(String phone, String user_from, String openid, String unionid, String code) {
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("user_phone", phone);
        paramsMap.put("openid", openid);
        paramsMap.put("code", code);
        if ("wx".equals(user_from)) {
            paramsMap.put("unionid", unionid);
            paramsMap.put("user_from", "2");

        } else if ("qq".equals(user_from)) {
            paramsMap.put("user_from", "1");
        }
        L.e(gson.toJson(paramsMap));
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getThirdLoginBindData(requestBody)
                .compose(XApi.<ThirdLoginBindData>getApiTransformer())         //异常处理
                .compose(XApi.<ThirdLoginBindData>getScheduler())              //线程切换
                .compose(getV().<ThirdLoginBindData>bindToLifecycle())
                .subscribe(new ApiSubcriber<ThirdLoginBindData>() {
                    @Override
                    protected void onFail(NetError error) {
                        L.e(error.toString());
                    }

                    @Override
                    public void onNext(ThirdLoginBindData thirdLoginBindData) {
                        if ("0".equals(thirdLoginBindData.getError_code())) {
                            SharedPref.getInstance(App.getInstance()).putString(Constant.TOKEN, thirdLoginBindData.getData().getToken());
                            ThirdLoginBindData.DataBean dataBean = thirdLoginBindData.getData();
                            UserInfo userInfo = new UserInfo();
                            userInfo.setToken(dataBean.getToken());
                            userInfo.setBirthday(dataBean.getBirthday());
                            userInfo.setUser_logo(dataBean.getUser_logo());
                            userInfo.setUser_nickname(dataBean.getUser_nickname());
                            userInfo.setUser_phone(dataBean.getUser_phone());
                            userInfo.setUser_sex(dataBean.getUser_sex());
                            userInfo.setEid(dataBean.getEid());
                            userInfo.setAge(dataBean.getAge());
                            updateDB(userInfo);
                            getV().getBindSucceed(thirdLoginBindData);
                        } else if ("1000".equals(thirdLoginBindData.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getBindFail(thirdLoginBindData.getMsg());
                        }
                    }
                });
    }

}
